﻿using Helper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http; 

namespace Langchur.Controllers
{
    public class APPController : ApiController
    {
        public class APPVersion
        {
            public String VersionNo { set; get; }
            public Int32 PatchNo { set; get; }
            public String Url { set; get; } 
            public String Description { set; get; }

            public String UpdateType { set; get; }

            private List<APPVersionPath> _Patchs = new List<APPVersionPath>();
            public List<APPVersionPath> Patchs
            {
                set { _Patchs = value; }
                get { return _Patchs; }
            }
        }

        public class APPVersionPath
        {
            public Int32 VersionNo { set; get; }
            public String Description { set; get; }
            public String Url { set; get; }
        }

        [HttpPost]
        public HttpResponseMessage Version(APPVersion AV)
        {
            DataManager dm = new DataManager();
            DataTable AllMainVersion = dm.SqlCmd("SELECT * FROM [dbo].[APP_Version] WHERE PID = 0 AND Src IS NOT NULL ORDER BY ID DESC").Tables[0];
            String[] Vs = AV.VersionNo.Split('.');
            String CurrentNo = "";
            Int32 MainId = 0;
            for(Int32 i = 0; i < 3; i++)
            {
                Int32 V1 = Int32.Parse(Vs[i]);
                CurrentNo += V1 + ".";
                Boolean found = false;
                foreach(DataRow dr in AllMainVersion.Rows)
                {
                    Int32 MV1 = Int32.Parse(dr["VersionNo"].ToString().Split('.')[i]);
                    if(MV1 > V1)
                    {
                        MainId = Int32.Parse(dr["ID"].ToString());
                        AV.VersionNo = dr["VersionNo"].ToString();
                        AV.PatchNo = 0;
                        AV.Url = dr["Src"].ToString();
                        AV.Description = dr["Description"].ToString();
                        AV.UpdateType = "Version";
                        found = true;
                        break;
                    } 
                }
                if (found)
                {
                    break;
                }
            }
            if(MainId == 0)
            {
                CurrentNo = CurrentNo.Substring(0, CurrentNo.Length - 1);
                DataTable PatchVersions = dm.SqlCmd("select * from APP_Version WHERE PID = (SELECT ID FROM APP_Version WHERE VersionNo = '" + CurrentNo + "') AND Src IS NOT NULL ORDER BY ID").Tables[0];
                //没有找到更新的大版本
                foreach(DataRow dr in PatchVersions.Rows)
                {
                    if (Int32.Parse(dr["VersionNo"].ToString()) > AV.PatchNo)
                    {
                        AV.UpdateType = "Build";
                        APPVersionPath AP = new APPVersionPath();
                        AP.Url = dr["Src"].ToString();
                        AP.Description = dr["Description"].ToString();
                        AP.VersionNo = Int32.Parse(dr["VersionNo"].ToString()); 
                        AV.Patchs.Add(AP);
                    }
                } 
            }
            return CResponse.ReturnSuccess(Newtonsoft.Json.JsonConvert.SerializeObject(AV));


        }
    }
}